<%
    /** 
     * 打印标题
     */
%>
<%
    logger.info("构造标题头区");
    ItemTitleArea itArea = (ItemTitleArea)request.getAttribute("viewer");
    logger.debug("  -- 取标题头区");
    List titles = itArea.getTitles(request.getContextPath());
    List realTitles = new ArrayList();
    logger.debug("  -- 显示标题头");
    if(titleSorts == null)
    {
        for(int i = 0; i < titles.size(); i ++)
        {
            Title title = (Title)titles.get(i);
            if(title.getPrinted())
            {
                realTitles.add(title);
            }
        }
    }
    else
    {
        for(int j = 0; j < titleSorts.length; j ++)
        {
            Title title = (Title)titles.get(Integer.parseInt(titleSorts[j]));
            if(title.getPrinted())
            {
                realTitles.add(title);
            }
        }
    }

    // 开始取最大深度和最大叶子数
    Title rootTitle = new Title();
    rootTitle.setSubTitles(realTitles);
    int maxDepth = rootTitle.getTreeDepth() - 1;
    int sumOfLeafs = rootTitle.getMaxSumOfSubTitlesLeafs();
    logger.debug("maxDepth: " + maxDepth);
    logger.debug("sumOfLeafs: " + sumOfLeafs);
    // 列大小控制
    %><colgroup><%
     for(int i = 0; i < rootTitle.getSumOfLeafs(); i ++)
     {
         %><col style="width:100"><%
     }
     %></colgroup><%
    // 代码
    StringBuffer[] htmlCodes = new StringBuffer[maxDepth];
    for(int i = 0; i < htmlCodes.length; i ++)
    {
        htmlCodes[i] = new StringBuffer(1000);
    }
    this.makeTitleHtmlCode(realTitles,htmlCodes,maxDepth,sumOfLeafs);
    for(int i = 0; i < htmlCodes.length; i ++)
    {
        out.print("<tr style='HEIGHT: 21px' bgcolor='#CACACA'>" + htmlCodes[i].toString() + "</tr>");
    }// for
    logger.info("outPrintTitles.jsp 完毕");
%>

<%! // 构造树状标题头
    private void makeTitleHtmlCode(List realTitles, // 标题树 集合
                                   StringBuffer[] htmlCodes, // 代码存放器（一个tr一个StringBuffer）
                                   int maxDepth, // 最大树深
                                   int sumOfLeafs// 最大叶子数，好像用不上
                                   )
    {
            // 开始打印标题
            for(int i = 0; i < realTitles.size(); i ++)
            {
                Title title = (Title)realTitles.get(i);                
                int itsSumOfLeafs = title.getSumOfLeafs();
                int rows = 0;
                if(title.getTreeDepth() > 0)
                {
                    rows = 1;
                }
                else
                {
                    rows = maxDepth - title.getTreeHighth();
                }
                StringBuffer htmlCode = htmlCodes[title.getTreeHighth()];
                if(itsSumOfLeafs == 0)
                    itsSumOfLeafs = 1;
                htmlCode.append("<td align='center' rowspan='" + rows + "' colspan='" + itsSumOfLeafs + "' align='center'>");
                htmlCode.append(title.getName());
                htmlCode.append("</td>");               
                if(title.getSubTitles().size() > 0)
                {
                    this.makeTitleHtmlCode(title.getSubTitles(),htmlCodes,maxDepth,sumOfLeafs);
                }
            }// for
    }
%>
